<?php
	$sendNewsletter = new Plugin('Send Newsletter');
	$sendNewsletter->Author('Ramblingwood, USA.');
	$sendNewsletter->Description( 'This allows you to send HTML email to people who have signed up.' );
	$sendNewsletter->version( '0.1' );
	$sendNewsletter->addInterface('sendNewsletter_Interface');
	$sendNewsletter->addInstall('sendNewsletter_Install');
	//$sendNewsletter->addPlugin( 'after-class-init', 'testAndRedirect' );
	//$sendNewsletter->addUninstall('sendNewsletter_Uninstall');
	
	function sendNewsletter_Install () {
		global $sendNewsletter;
		$form = <<<EOT
	<form action="%%visual_root%%%%page_slug%%" method="post">
		<fieldset>
			%%message%%
			<input type="text" name="email" value="%%emailValue%%" /><br />
			<input type="text" name="name" value="%%nameValue%%" /><br />
			<input type="submit" name="submit" value="Signup" /><br />
		</fieldset>
	</form>
EOT;
		$sendNewsletter->updateOption('newsletterOptions', array(
			'Confimation Page Title' => 'Newsletter',
			'Sidebar Block Title'    => 'Signup for a newsletter!',
			'Sidebar Block Content'  => $form,
			'Sidebar Block Message'  => 'You can signup for our newsletter here.'
		));
		$sendNewsletter->updateOption('newsletterSignees', array());
		$sendNewsletter->addPage('Newsletter', 'Newsletter', '<?php echo checkEmailAndSignup(); ?>', '0', '0', '', array(
			'show' => 'hidden'
		));
	}
	
	class newsletter {
		public $options;
		public function __construct () {
			$this->setOptions();
		}
		private function setOptions () {
			global $sendNewsletter;
			$this->options = $sendNewsletter->getOption('newsletterOptions');
			foreach((array) $this->options as $key => $value) {
				$this->options[$key] = stripcslashes(stripcslashes($value));
			}
		}
		public function getOption($opt) {
			return $this->options[$opt];
		}
		public function setOption ($key, $value) {
			global $sendNewsletter;
			$this->options[$key] = $value;
			$sendNewsletter->updateOption('newsletterOptions', $this->options);
		}
		public function displaySidebar ($emailValue = 'Your Email', $nameValue = 'Your Name') {
			global $sendNewsletter, $theme;
			$form    = $this->getOption('Sidebar Block Content');
			$message = $this->getOption('Sidebar Block Message');
			$title   = $this->getOption('Confimation Page Title');
			//var_dump($theme->hidden, $theme->idLookup($title, true, false, true));
			$form = str_replace(
							array(
								'%%page_slug%%',
								'%%visual_root%%',
								'%%message%%',
								'%%emailValue%%',
								'%%nameValue%%'
							), array(
								CONTENT_SLUG.'/'.$theme->make_clean_url($title),
								$sendNewsletter->VISUAL_ROOT,
								$message,
								$emailValue,
								$nameValue
							), $form);
			echo $form;
		}
	}
	
	$newsletter = new newsletter();
	
	function sendNewsletter_sidebar () {
		global $newsletter;
		$newsletter->displaySidebar();
	}
	
	function sendNewsletter_Interface () {
		global $newsletter, $sendNewsletter, $page;
		if($_POST['delete']) {
			$peeps = $sendNewsletter->getOption('newsletterSignees');
			unset($peeps[$_POST['delEmail']]);
			$sendNewsletter->updateOption('newsletterSignees', $peeps);
			header('location: admin.php?page=plugins&interface=send-newsletter&mess=1');
		}
		if($_POST['save']) {
			$newsletter->setOption('Sidebar Block Content', $_POST['html']);
			echo $page->message('success', 'HTML Saved', 'Your HTML was saved!');		
		}
		if($_GET['mess'] == '1') {
			echo $page->message('success', 'Email Deleted', 'That person was removed from the email list');		
		}
		$peeps = $sendNewsletter->getOption('newsletterSignees');
		if(count((array) $peeps) > 0) {
		foreach($peeps as $email => $name) {
			$biglist .= $name." &lt;$email&gt;, ";
			$deleteList .= "<option value='$email'>$name &lt;$email&gt;</option>";
		}
		$biglist = rtrim($biglist, ', ');
		echo <<<EOT
	<form action="admin.php?page=plugins&interface=send-newsletter" method="post">
		<fieldset>
			<h2>Email List</h2>
			<p>Copy an paste this into your email client to send the email</p>
			<code>$biglist</code>
		</fieldset>
	</form>
	<form action="admin.php?page=plugins&interface=send-newsletter" method="post">
		<fieldset>
			<h2>Delete an Email</h2>
			<div class="form-row">
				<label for="delEmail">Email Address</label>
				<span>
					<select name="delEmail">$deleteList</select>
				</span>
			</div>
			<div class="form-row form-row-last">
				<label for="delete">&nbsp;</label>
				<span>
					<input name="delete" type="submit" value="Delete" />
				</span>
			</div>
		</fieldset>
	</form>
EOT;
		}
		else {
			echo $page->message('warning', 'No one has signed up!', 'There is no one in your mailing list!');		
		}
	}
	
	function checkEmailAndSignup() {
		global $newsletter, $sendNewsletter, $page;
		if($_POST['email']) {
			if(!validEmail($_POST['email']) || empty($_POST['email']))
				$errors['Invalid Email'] = 'The email you entered was invalid. Please try again';
			if(empty($_POST['name']))
				$errors['Empty Name'] = 'Please do not leave the name field empty.';
			if(count((array) $errors) > 0) {
				foreach($errors as $key => $value) {
					echo $page->message('error', $key, $value);
				}
				$newsletter->displaySidebar($_POST['email'], $_POST['name']);
			}
			else {
				$currentSignees = (array) $sendNewsletter->getOption('newsletterSignees');
				if(array_key_exists($_POST['email'], $currentSignees)) {
					echo $page->message('warning', 'You are already signed up!', 'You should be recieving your emails already!');
				}
				else {
					$currentSignees[$_POST['email']] = $_POST['name'];
					$sendNewsletter->updateOption('newsletterSignees', $currentSignees);
					echo $page->message('success', 'You signed up!', 'You should recieve your first email in a few days.');
				}
			}
			//var_dump($_POST);
		}
		else {
			$newsletter->displaySidebar();
		}
	}
	
	$sendNewsletter->addSidebarBlock($newsletter->options['Sidebar Block Title'], 'sendNewsletter_sidebar');

?>